<?
include('../../src/inst.conf.php');

$cfg['db']['host'] = "localhost";
$cfg['db']['user'] = "root";
$cfg['db']['password'] = "";
$cfg['db']['database'] = "groenhorst_college";
//Conect to the database 
mysql_connect($cfg['db']['host'], $cfg['db']['user'], $cfg['db']['password']);
//Select Database
mysql_select_db($cfg['db']['database']);
?>


<!-- JavaScript -->
<script src="./js/jquery.min.js"></script>
<script src="./js/jqueryui.min.js"></script>
<script src="./js/jquery.cookies.js"></script>
<script src="./js/jquery.pjax.js"></script>
<script src="./js/formalize.min.js"></script>
<script src="./js/jquery.metadata.js"></script>
<script src="./js/jquery.validate.js"></script>
<script src="./js/jquery.checkboxes.js"></script>
<script src="./js/jquery.chosen.js"></script>
<script src="./js/jquery.fileinput.js"></script>
<script src="./js/jquery.datatables.js"></script>
<script src="./js/jquery.sourcerer.js"></script>
<script src="./js/jquery.tipsy.js"></script>
<script src="./js/jquery.calendar.js"></script>
<script src="./js/jquery.inputtags.min.js"></script>
<script src="./js/jquery.wymeditor.js"></script>
<script src="./js/jquery.livequery.js"></script>
<script src="./js/jquery.flot.min.js"></script>
<script src="./js/jquery.watch.js"></script>
<script src="./js/application.js"></script>
<script type="text/javascript">
    $('#modalcontainer').html('<div id="edit_docent" class="box"></div>');
    var tr = "";
    var modal = $("#edit_docent");
    $('a[href="#edit_docent"]').live('click', function () {
        $('#edit_docent').html('<div class="box-header"><h1>Bewerk docent</h1></div><div class="box-content"><form name="edit_docent_form" id="edit_docent_form" action="javascript:;"><div class="column-left"><p><input type="hidden" id="id" name="id"><span class="icon tick valid"></span></p><p><input type="text" id="Voornaam" name="Voornaam" placeholder="Voornaam" class="{validate: {required:true}}"><span class="icon tick valid"></span></p><p><input type="text" id="tussen" name="tussen" placeholder="Tussenvoegsel" class="{validate: {required:false}}"><span class="icon tick valid"></span></p><p><input type="text" id="Achternaam" name="Achternaam" placeholder="Achternaam" class="{validate: {required:true}}"><span class="icon tick valid"></span></p></div><div class="column-right"><p><input type="text" id="Gebruikersnaam" name="Gebruikersnaam" placeholder="Gebruikersnaam" class="{validate: {required:true}}"><span class="icon tick valid"></span></p><p><input type="text" id="Email" name="Email" placeholder="E-Mail adres" class="{validate: {required:true}}"><span class="icon tick valid"></span></p><p><input type="checkbox" name="admin" id="admin" /><label for="admin">Administrator</label></p><p> <input type="checkbox" name="disabled" id="disabled" /> <label for="disabled">Uitgeschakeld</label></p></div><div class="clear"></div><div class="action_bar"><input type="submit" class="button red" value="Submit" id="submit_edit_docent" name="submit_edit_docent"><a href="#" onclick="$(\'#overlay\').hide();"class="close button">Close window</a></div></form></div>');
        $('#overlay').show();
        $('#edit_docent').show();
        tr = $(this).parent().parent();
        $(modal).find('[id="id"]').val($(tr).find("td:nth-child(1)").html());
        $(modal).find('[id="Voornaam"]').val($(tr).find("td:nth-child(2)").html());
        $(modal).find('[id="tussen"]').val($(tr).find("td:nth-child(3)").html());
        $(modal).find('[id="Achternaam"]').val($(tr).find("td:nth-child(4)").html());
        $(modal).find('[id="Gebruikersnaam"]').val($(tr).find("td:nth-child(5)").html());
        $(modal).find('[id="Email"]').val($(tr).find("td:nth-child(6)").html());
        if ($(tr).find("td:nth-child(7)").html() == "Administrator") {
            $(modal).find('[id="admin"]').attr("checked", "checked");
        } else if ($(tr).find("td:nth-child(7)").html() == "[geblokkeerd] Administrator") {
            $(modal).find('[id="admin"]').attr("checked", "checked");
            $(modal).find('[id="disabled"]').attr("checked", "checked");
        } else if ($(tr).find("td:nth-child(7)").html() == "[geblokkeerd] Gebruiker") {
            $(modal).find('[id="disabled"]').attr("checked", "checked");
        }

        $('input[type=checkbox],input[type=radio]').prettyCheckboxes();
    });
    $("#submit_edit_docent").live('click', function () {
        var table = $('#docenten_datatable').dataTable();
        $(tr).find("td:nth-child(2)").html($(modal).find('[id="Voornaam"]').val());
        $(tr).find("td:nth-child(3)").html($(modal).find('[id="tussen"]').val());
        $(tr).find("td:nth-child(4)").html($(modal).find('[id="Achternaam"]').val());
        $(tr).find("td:nth-child(5)").html($(modal).find('[id="Gebruikersnaam"]').val());
        $(tr).find("td:nth-child(6)").html($(modal).find('[id="Email"]').val());
        var rights = "";
        if ($(modal).find('[id="disabled"]').is(':checked')) {
            $(tr).addClass("disabled");
            rights = "[geblokkeerd] ";
        } else {
            $(tr).removeClass("disabled");
        }
        if ($(modal).find('[id="admin"]').is(':checked')) {
            rights = rights + "Administrator";
        } else {
            rights = rights + "Gebruiker";
        }
        $(tr).find("td:nth-child(7)").html(rights);
        table.fnDestroy();
        var table = $('#leerlingen_datatable').dataTable();
        showLoader('Opslaan..', 'Gegevens worden opgeslagen in de database');
        var send_data = $("#edit_docent_form").serializeArray();
        var url = "./src/edit.docent.php";
        $.post(url, send_data, function (data) {
        });
        $('#overlay').hide();
    });
    $('a[href="#remove"]').live('click', function () {
        //<!---$('span#total').html($('span#total').html() - 1);---! >
        var url = "./src/remove.docent.php";
        $.post(url, "id=" + $(this).parent().parent().find('td:nth-child(1)').html(), function (data) {
        });
        var table = $('#docenten_datatable').dataTable();
        table.fnDeleteRow($(this).parent().parent());

    });
</script>
<div class="box">
    <div class="box-header">
        <h1>Docenten</h1>
    </div>

    <table id="docenten_datatable" class="datatable">
        <thead>
        <tr>
            <th>ID</th>
            <th>Voornaam</th>
            <th width="50px"></th>
            <th>Achternaam</th>
            <th>Gebruikersnaam</th>
            <th>Email</th>
            <th>Rechten</th>
            <th width="200px">Acties</th>
        </tr>
        </thead>
        <tbody>
        <?
        // haal alle docenten uit de database
        $query = mysql_query("SELECT * FROM  `DOCENT`");
        while ($data = mysql_fetch_assoc($query)) {
            $rechten = "";
            if (isset($data['DISABLED']) && $data['DISABLED'] == 1) {
                echo"<tr class=\"disabled\">";
                @$rechten .= "[geblokkeerd] ";
            } else {
                echo"<tr>";
            }
            if ($data['ADMIN'] == 1) {
                @$rechten .= "Administrator";
            } else {
                @$rechten .= "Gebruiker";
            }
            echo"  <td>" . $data['DOCENT_ID'] . "</td>";
            echo"  <td>" . utf8_encode($data['VOORNAAM']) . "</td>";
            echo"  <td class=\"sorting_disabled\">" . $data['TUSSENVOEGSEL'] . "</td>";
            echo"  <td>" . utf8_encode($data['ACHTERNAAM']) . "</td>";
            echo"  <td>" . utf8_encode($data['GEBRUIKERSNAAM']) . "</td>";
            echo"  <td>" . utf8_encode($data['E_MAIL_ADRES']) . "</td>";
            echo"  <td>" . @$rechten . "</td>";
            echo"  <td>";
            echo"  	<a href=\"#edit_docent\" class=\"button plain\">Wijzig</a>";
            echo"  	<a href=\"#remove\" class=\"button plain red\">Verwijder</a>";
            echo"  </td>";
            echo"</tr>";
        }

        ?>
        </tbody>
    </table>
</div> 